package com.dk.controller;

import com.dk.RES;
import com.dk.service.ChatService;
import com.dk.ws.bean.Message;
import com.jack.weChatSecurity.core.SecurityHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api")
public class ChatRecordController {

    @Autowired
    ChatService chatService;

    @GetMapping("/chatRecord")
    @ResponseBody
    public RES getChatRecord(@RequestParam("destinationId")String destinationId){
        RES res=new RES();
        try{
            String id= SecurityHelper.getWeChatUser().getId();
            List<Message> messages=chatService.getChatRecord(id,destinationId);
            res.setCode(0).setMsg("ok").addData("messages",messages);
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("error");
        }finally {
            return res;
        }
    }

    @DeleteMapping("/chatRecord")
    @ResponseBody
    public RES deleteChatRecord(@RequestParam("destinationId")String destinationId){
        RES res=new RES();
        try{
            String id= SecurityHelper.getWeChatUser().getId();
            chatService.deleteChatRecord(id,destinationId);
            res.setCode(0).setMsg("删除聊天记录成功");
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("删除聊天记录失败");
        }finally {
            return res;
        }
    }
}
